﻿/**
 * 
 * F. Mehmet ARSLAN (c) 2015
 * fmehmetarslan46@gmail.com
 *  
 **/
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace ServiceState.Controllers
{
    public class WidgetController : Controller
    {
        //
        // GET: /Widget/

        public ActionResult Jenkins()
        {
            String jenkinsUrl = ConfigurationManager.AppSettings["jenkinsUrl"];
            if (String.IsNullOrEmpty(jenkinsUrl))
            {
                Response.Write("webconfig içerisinde jenkinsUrl parametresini tanımlayınız");
                return View("widget");
            }
            StringBuilder sb = new StringBuilder();
            HttpWebResponse response = createRequest(jenkinsUrl + "/ajaxExecutors");
            if (response != null && response.GetResponseStream() != null)
            {
                using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    sb.Append(sr.ReadToEnd());
                }
                response = createRequest(jenkinsUrl + "/ajaxBuildQueue");
                using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    sb.Append(sr.ReadToEnd());
                }
                sb.Replace("href=\"", "target='_blank' href=\"" + jenkinsUrl);
                sb.Replace("src=\"", "src=\"" + jenkinsUrl);
                sb.Replace("href='", "target='_blank' href='" + jenkinsUrl);
                sb.Replace("src='", "src='" + jenkinsUrl);
                Response.Write(sb.ToString());
            }
            return View("widget");
        }
        public ActionResult appwiki()
        {
            String appwikiUrl = ConfigurationManager.AppSettings["appwikiUrl"];
            if (String.IsNullOrEmpty(appwikiUrl))
            {
                Response.Write("webconfig içerisinde appwikiUrl parametresini tanımlayınız");
                return View("widget");
            }
            StringBuilder sb = new StringBuilder();
            HttpWebResponse response = createRequest(appwikiUrl + "/pages/allpages");
            if (response != null && response.GetResponseStream() != null)
            {
                using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    sb.Append(sr.ReadToEnd());
                }
                HtmlAgilityPack.HtmlDocument htmldoc = new HtmlAgilityPack.HtmlDocument();
                htmldoc.LoadHtml(sb.ToString());
                HtmlNode table = htmldoc.DocumentNode.SelectSingleNode("//*[@id='content']/table");
                String htmltext = table.OuterHtml;
                htmltext = htmltext.Replace("href=\"", "target='_blank' href=\"");
                htmltext = htmltext.Replace("href='", "target='_blank' href='");
                Response.Write(htmltext);
            }
            return View("widget");
        }

        public ActionResult sonar()
        {

            String sonarUrl = ConfigurationManager.AppSettings["sonarUrl"];
            if (String.IsNullOrEmpty(sonarUrl))
            {
                Response.Write("webconfig içerisinde appwikiUrl parametresini tanımlayınız");
                return View("widget");
            }
            StringBuilder sb = new StringBuilder();
            HttpWebResponse response = createRequest(sonarUrl + "/components/index/1240");
            if (response != null && response.GetResponseStream() != null)
            {
                using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    sb.Append(sr.ReadToEnd());
                }
                HtmlAgilityPack.HtmlDocument htmldoc = new HtmlAgilityPack.HtmlDocument();
                htmldoc.LoadHtml(sb.ToString());
                HtmlNode table = htmldoc.DocumentNode.SelectSingleNode("//*[@id='components']");
                String htmltext = table.OuterHtml;
                htmltext = htmltext.Replace("href=\"", "target='_blank' href=\"" + sonarUrl);
                htmltext = htmltext.Replace("href='", "target='_blank' href='" + sonarUrl);
                Response.Write(htmltext);
            }
            return View("widget");
        }

        private HttpWebResponse createRequest(String url)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["username"]??"app2", ConfigurationManager.AppSettings["pass"]??"appyeni", ConfigurationManager.AppSettings["domain"]??"MILENET");
                WebResponse response = request.GetResponse();
                return ((HttpWebResponse)response);
            }
            catch (System.Net.WebException wexc)
            {
                return ((HttpWebResponse)wexc.Response);
            }
        }
    }
}
